package com.niit.dao;

import com.niit.pojo.downFile;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Liu
 * @date 2022/2/13 13:26
 **/

public class displayFiles {

    public List<downFile> displayFiles() throws IOException {
        Configuration conf = new Configuration();
        String resourceUrl = "D:\\Hbase学习\\hadoop-2.7.3";
        String url = "hdfs://192.168.40.133:9000";
        System.setProperty("hadoop.home.dir", resourceUrl);
        Path origin = new Path("/Niit/");// hdfs目录树下的/mktest目录
        FileSystem fileSystem = FileSystem.get(URI.create(url), conf);
        RemoteIterator<LocatedFileStatus> status = fileSystem.listFiles(origin, true);
        List<downFile> downFiles = new ArrayList<>();
        while (status.hasNext()) {
            LocatedFileStatus fileStatus = status.next();
//            System.out.println("路径:"+fileStatus.getPath());
//            System.out.println("副本数:"+fileStatus.getReplication());
//            System.out.println("文件大小:"+fileStatus.getLen());
//            System.out.println();
            downFile downFile = new downFile();
            downFile.setFile_url(fileStatus.getPath().toString());
            downFile.setFilename(fileStatus.getPath().toString().substring(32));
            downFile.setFilesize(String.valueOf(fileStatus.getLen()));
//            System.out.println(downFile.toString());
            downFiles.add(downFile);
        }
        return downFiles;
    }


    public static void main(String[] args) throws IOException {
        displayFiles displayFiles = new displayFiles();
        displayFiles.displayFiles();
    }
}
